import { _recommend } from '../../common/projects'
import { NAME_REG, TEL_REG } from '../../utils/reg'
const app = getApp()
Page({
  data: {
    pid: '',
    mid:'',
    name: '',
    tel: '',
    remark: '',
    industries: ['IT科技', '金融业', '餐饮', '酒店旅游', '运输与仓储', '教育', '政府与公共事业', '医疗护理', '交通工具', '房地产', '消费品', '商业服务', '文体娱乐', '印刷', '其他'],
    industry: null,
    areas: ['200平米以内', '200 - 400平米', '400 - 1000平米', '1000平米以上'],
    area: null
  },
  onPickerChange (e) {
    let attr = e.currentTarget.dataset.attr
    this.data[attr] = e.detail.value
    this.setData({
      [attr]: this.data[attr]
    })
  },
  inputHandler (e) {
    let attr = e.currentTarget.dataset.attr
    this.data[attr] = e.detail
  },
  textChange (e) {
    this.data.remark = e.detail.value
  },
  submit () {
    const { pid, mid, name, tel, remark, industries, industry, areas, area } = this.data
    const profession = industry === null ? '' : industries[industry]
    const area1 = area === null ? '' : areas[area]
    if (!pid) {
      app.toast('项目ID异常')
      return
    }
    if (!NAME_REG.test(name)) {
      app.toast('请填写2-6位中文姓名')
      return
    }
    if (!TEL_REG.test(tel)) {
      app.toast('请填写正确格式的手机号码')
      return
    }
    if (!profession) {
      app.toast('请选择行业')
      return
    }
    if (!area1) {
      app.toast('请选择所需面积')
      return
    }
    app.loading('加载中')
    _recommend(
      pid,
      mid,
      name,
      tel,
      remark,
      profession,
      area1
    ).then(res => {
      wx.hideLoading()
      wx.showModal({
        title: res.data.IsSuccess?'温馨提示':'对不起',
        content: res.data.Msg,
        showCancel: false,
        success: r => {
          if (res.data.IsSuccess) {
            wx.navigateBack()
          }
        }
      })
    }).catch(err => {
      wx.hideLoading()
      console.log(err)
      wx.showModal({
        title: '对不起',
        content: '请求失败，请稍后再试！',
        showCancel: false
      })
    })
  },
  onLoad (options) {
    this.data.pid = options.pid
  },
  onReady () {},
  onShow () {
    app.memberReadyCb = () => {
      this.data.mid = app.globalData.member.ID
      this.setData({
        mname: app.globalData.member.Name,
        mtel: app.globalData.member.Tel
      })
    }
    app.fansReadyCb = () => {
      app.checkMember()
    }
    app.init()
  },
  onHide () {},
  onUnload () {},
  onPullDownRefresh () {},
  onReachBottom () {},
  onShareAppMessage() {
    return app.shareInfo
  }
})